package com.learn.bytedance;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 队列满的时候 消费者开始消费 (生产者进入等待池)
 * 队列为空的时候  生产者开始生产
 */
public class Storage {

    private Integer MAX_QUEUE = 10;

    private Queue<Integer> queue = new LinkedList<>();

    private AtomicInteger i = new AtomicInteger(0);

    public void produce() {
        synchronized (queue) {
            try {
                if (queue.size() == 10) {
                    System.out.println("队列已满");
                    queue.wait();
                }
                queue.offer(i.incrementAndGet());
                System.out.println("--生产者创建信息-->" + i);
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void consume() {
        synchronized (queue) {
            try {
                if (queue.size() == 0) {
                    System.out.println("消费队列为空，等待消息");
                    queue.wait();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
